public class towork2 {
    //力扣105. 从前序与中序遍历序列构造二叉树
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int last=inorder.length-1;
        TreeNode root= bianTree(preorder,inorder,0,last);
        return root;
    }
    public int post=0;
    //通过preorder的元素一个一个去inorder 并从该点作为新的根进行递归
    public TreeNode bianTree(int[] preorder,int[]inorder,int beign,int length){
        if(beign>length){//beign不能取=length   下面index-1==beign 是可以行的不能 null
            return null;
        }
        TreeNode root=new TreeNode(preorder[post]);
        int index=find(inorder,beign,length,preorder[post]);
        post++;
        root.left=bianTree(preorder,inorder,beign,index-1);
        root.right=bianTree(preorder,inorder,index+1,length);
        return root;
    }
    public int find(int[] inorder,int beign,int length,int key){
        for(int i=beign;i<=length;i++){
            if(inorder[i]==key){
                return i;
            }
        }
        return -1;
    }
}
